<template>
  <div class="article-container">
    <el-card>
      <!-- 搜索框 -->
      <el-form :model="listInfo" class="article-form" ref="articleRef" label-width="60px">
        <el-form-item label="关键字">
          <el-input v-model="listInfo.keyword" clearable @clear="getArticle"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="listInfo.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="search-article">
          <el-button @click="clear">清除</el-button>
        <el-button type="primary" @click="getArticle">搜索</el-button>
        </el-form-item>

        <el-form-item class="add-article">
          <el-button type="success" icon="el-icon-edit" @click="AddDialogVisible=true">新增技巧</el-button>
        </el-form-item>
      </el-form>
      <el-alert :title="`数据一共${count}条`" type="info" class="alert" :closable="false" show-icon></el-alert>
      <!-- 面试技巧列表 -->
      <el-table :data="articleList" style="width: 100%" stripe>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="文章标题" width="400">
          <template v-slot="scope">
            <div>
              <span>{{scope.row.title}}</span>
              <i
                class="el-icon-video-camera-solid"
                @click="videoShow=true ;videoURL=scope.row.videoURL"
                v-if="scope.row.videoURL"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column label="录入时间">
          <template v-slot="scope">{{scope.row.createTime | parseTimeByString("{y}-{m}-{d}")}}</template>
        </el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">{{scope.row.state===1?'已启用':'已禁用'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button type="text" @click="onPreview(scope.row)">预览</el-button>
            <el-button
              type="text"
              @click="changeArticleState(scope.row)"
            >{{scope.row.state===1?'禁用':'启用'}}</el-button>
            <el-button
              @click="editDialogShow(scope.row)"
              type="text"
              :disabled="scope.row.state===1"
            >修改</el-button>
            <el-button
              @click="removeArticle(scope.row)"
              type="text"
              :disabled="scope.row.state===1"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="listInfo.page"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="listInfo.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="count"
      ></el-pagination>
      <!-- 预览文章对话框 -->
      <el-dialog title="预览文章" :visible.sync="previewDialogVisible" width="800px">
        <articles-preview :previewInfo="previewInfo" v-if="previewDialogVisible"></articles-preview>
      </el-dialog>
      <!-- 新增技巧对话框 -->
      <el-dialog title="新增文章" :visible.sync="AddDialogVisible">
        <articles-add v-if="AddDialogVisible" @addA="addArticle"></articles-add>
      </el-dialog>
      <!-- 修改技巧对话框 -->
      <el-dialog title="修改文章" :visible.sync="editDialogVisible" width="70%">
        <article-edit @editA="editArticle" v-if="editDialogVisible" :oldData="editInfo"></article-edit>
      </el-dialog>
    </el-card>
    <!-- 视频 -->
      <article-video :videoURL="videoURL" @closeVideo="videoShow=false" class="video" v-if="videoShow"></article-video>
  </div>
</template>

<script>
import { list, remove, update, add, changeState } from '@/api/hmmm/articles'
import articlesPreview from '../components/articles-preview.vue'
import ArticlesAdd from '../components/articles-add.vue'
import ArticleEdit from '../components/article-edit.vue'
import ArticleVideo from '../components/article-video.vue'
export default {
  data () {
    return {
      // 表格渲染参数
      listInfo: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      // 面试文章表格数据
      articleList: [],
      // 下拉框选项
      options: [
        {
          value: null,
          label: '不限制'
        },
        {
          value: 0,
          label: '禁用'
        },
        {
          value: 1,
          label: '启用'
        }
      ],
      // 文章总数
      count: 0,
      // 预览框展示
      previewDialogVisible: false,
      // 添加文章框展示
      AddDialogVisible: false,
      // 修改文章框展示
      editDialogVisible: false,
      // 视频展示
      videoShow: false,
      // 修改框所需数据
      editInfo: {
        id: null,
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 预览文章所需数据
      previewInfo: {},
      // 视频地址
      videoURL: ''
    }
  },
  components: {
    // 添加面试技巧
    ArticlesAdd,
    // 修改面试技巧
    ArticleEdit,
    // 视频
    ArticleVideo,
    // 预览面试技巧
    articlesPreview
  },
  created () {
    this.getArticle()
  },
  methods: {
    async getArticle () {
      const { data: res } = await list(this.listInfo)
      this.count = res.counts
      this.articleList = res.items
      console.log(res)
    },
    // 清空搜索框
    clear () {
      this.listInfo.keyword = ''
      this.listInfo.state = null
    },
    // 数据条数改变
    handleSizeChange (size) {
      this.listInfo.pagesize = size
      this.getArticle()
    },
    // 页码改变
    handleCurrentChange (num) {
      this.listInfo.page = num
      this.getArticle()
    },
    // 删除面试技巧
    removeArticle (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(data)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.getArticle()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 确认添加面试技巧
    async addArticle (data) {
      await add(data)
      this.$message({
        message: '添加成功',
        type: 'success'
      })
      this.AddDialogVisible = false
      this.getArticle()
    },
    // 改变文章状态
    async changeArticleState (obj) {
      try {
        await changeState({
          id: obj.id,
          state: obj.state === 0 ? 1 : 0
        })
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.getArticle()
      } catch (error) {
        this.$message({
          message: error,
          type: 'error'
        })
      }
    },
    // 点击修改时触发
    editDialogShow (data) {
      this.editInfo.id = data.id
      this.editInfo.title = data.title
      this.editInfo.articleBody = data.articleBody
      this.editInfo.videoURL = data.videoURL
      this.editDialogVisible = true
    },
    // 修改文章
    async editArticle (data) {
      await update(data)
      this.$message({
        message: '修改文章成功',
        type: 'success'
      })
      this.editDialogVisible = false
      this.getArticle()
    },
    onPreview (data) {
      this.previewDialogVisible = true
      this.previewInfo = data
    }
  }
}
</script>

<style scoped lang='less'>
.article-container {
  padding: 10px;
  .el-table {
    i {
      color: rgb(19, 144, 228);
      cursor: pointer;
    }
  }
  .el-card {
    position: relative;
  }
  .article-form{
    display: flex;
    flex-wrap: wrap;
    padding-left: 30px;
     .el-button {
    margin-left: 20px;
  }
    .search-article{
      margin-left: -60px;
    }
    .add-article{
      flex:1;
      justify-self: right;
      text-align: right;
    }
  }
  .el-alert {
    margin-bottom: 10px;
  }
  .pagination {
    float: right;
    margin: 20px 0;
  }
}
</style>
